package com.springboot.blog.controller;

import com.alibaba.fastjson.JSONObject;
import com.springboot.blog.beans.User;
import com.springboot.blog.beans.YouQing;
import com.springboot.blog.config.utils.R;
import com.springboot.blog.dao.YouQingDao;
import net.sf.json.JSONArray;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/admin")
public class YouQingController {

    @Resource
    private YouQingDao youQingDao;

    @RequestMapping("addyqLink")
    public R addyouqing(String yqTitle,String yqLink){
        YouQing youQing = new YouQing();
        youQing.setYqTitle(yqTitle);
        youQing.setYqLink(yqLink);
        int insert = youQingDao.insert(youQing);
        if (insert>0){
            return R.ok();
        }
        return R.error();
    }

    @RequestMapping("findyqLink")
    public String findyqLink(){
        List<YouQing> youQings = youQingDao.selectList(null);
        Integer count = youQingDao.selectCount(null);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("count",count);
        jsonObject.put("data",youQings);
        return jsonObject.toString();
    }

    @RequestMapping("deleteyq")
    public R deleteyq(String delete){
        List<YouQing> list2=(List<YouQing>) JSONArray.toList(JSONArray.fromObject(delete), YouQing.class);
        List<Integer> userList = new ArrayList<>();
        for (YouQing youQing : list2){
            userList.add(youQing.getYqId());
        }
        int deleteUser = youQingDao.deleteBatchIds(userList);
        if (deleteUser>0) {
            return R.ok();
        }
        return R.error();
    }

    @RequestMapping("deleteByIdYq")
    public R deleteByIdYq(String yqId){
        int delete = youQingDao.deleteById(Integer.parseInt(yqId));
        if (delete>0){
            return R.ok();
        }
        return R.error();
    }

    @RequestMapping("updateByIdYq")
    public R updateByIdYq(String yqId,String yqTitle,String yqLink){
        YouQing youQing = new YouQing();
        youQing.setYqId(Integer.parseInt(yqId));
        youQing.setYqTitle(yqTitle);
        youQing.setYqLink(yqLink);
        int update = youQingDao.updateById(youQing);
        if (update>0){
            return R.ok();
        }
        return R.error();
    }

}
